package com.schoolgard.action.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.schoolegard.services.CatalogService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.Catalog;
import com.schoolgard.entitybean.TreeDataModel;
import com.supersystem.utils.JSONUtils;
import com.supersystem.utils.NumberUtil;

public class CatalogAction extends BaseAction{
	
	private static Logger logger = Logger.getLogger(CatalogAction.class);

	private static final long serialVersionUID = 201111252321L;
	private CatalogService catalogService;
	private Catalog modCatalog ;
	public void setCatalogService(CatalogService catalogService) {
		this.catalogService = catalogService;
	}
	public void setModCatalog(Catalog modCatalog) {
		this.modCatalog = modCatalog;
	}
	public Catalog getModCatalog() {
		return modCatalog;
	}
	/**
	 * 查询菜单树
	 */
	public void queryList()
	{
		 String parentId = request.getParameter("catalogId");
		 writeToResponse(JSONUtils.toJSONString(queryCatalogListByParentId(parentId)));
	}
	
	/**
	 * 保存菜单节点
	 */
	public void saveCatalog()
	{
		String pCid = request.getParameter("pCId");
		String CId = request.getParameter("CId");
		String CEnName = request.getParameter("CEnName");
		String CCnName = request.getParameter("CCnName");
		String CLinkUrl = request.getParameter("CLinkUrl");
		String CMenuImg = request.getParameter("CMenuImg");
		String CIsleaf = request.getParameter("CIsleaf");
		String CSort = request.getParameter("CSort");
		String CTempId = request.getParameter("CTempId");
		
		Catalog catalog = null;
		if(StringUtils.isBlank(CId)){
			List<Catalog> catalogs = catalogService.findCatalogByParentId(pCid);
			if(!StringUtils.equals(pCid, "000")){
				if(catalogs == null || catalogs.size() == 0){
					CId = pCid+"001";
				}else{
					int maxCId = catalogService.findMaxCIdByParentId(pCid); //获取最大id
					CId = pCid+NumberUtil.formatToLengthString(maxCId+1, "000");
				}
			}else{
				CId = NumberUtil.formatToLengthString(catalogs.size()+1, "000");
			}
		   catalog = new Catalog();
		   catalog.setId(CId);
		}else{
			catalog = catalogService.findById(CId); 
		}
		catalog.setEnName(CEnName);
		catalog.setCnName(CCnName);
		catalog.setLinkUrl(CLinkUrl);
		catalog.setMenuImg(CMenuImg);
		catalog.setIsleaf(StringUtils.equals(CIsleaf, "1")?true:false );
		catalog.setSort(NumberUtil.paseStringToInt(CSort));
		String success = catalogService.saveOrUpdate(catalog);
		
		writeToResponse(success);
	}
	
	/**
	 * 删除菜单，会删除所有的子节点
	 */
	public void deleteCatalog()
	{
		String catalogId = request.getParameter("catalogId");
		if(StringUtils.isBlank(catalogId)) return;
		
		int success = delete(catalogId);
		writeToResponse(Integer.toString(success)) ;
	}
	
	public void findCatalog()
	{
		String catalogId = request.getParameter("catalogId");
	    modCatalog = catalogService.findById(catalogId);
		writeToResponse(JSONUtils.toJSONString(modCatalog));
	}
	
	public int delete(String catalogId)
	{
		Catalog catalog = catalogService.findById(catalogId);
		List<Catalog> children = catalogService.findCatalogByParentId(catalog.getId());
		int success = 1; //一个都没有删除则默认删除成功，以便删除空的父节点
		catalogService.delete(catalog); //先把自己删掉
		for (Catalog Catalog : children) {
			success = delete(Catalog.getId());
			if(success == 0) break;
		}
		return success;
	}
	
	public void findCatalogById()
	{
		String catalogId = request.getParameter("catalogId");
		if(StringUtils.isBlank(catalogId)) return;
		TreeDataModel catalogTree = new TreeDataModel();
		Catalog catalog = catalogService.findById(catalogId);
		catalogTree.setId(catalog.getId());
		catalogTree.setText(catalog.getCnName());
		catalogTree.setChildren(queryCatalogListByParentId(catalog.getId()));
		writeToResponse(JSONUtils.toJSONString(catalogTree));
	}
	
	public List<TreeDataModel> queryCatalogListByParentId(String parentId){
		if(StringUtils.isBlank(parentId)){
			parentId = "000";  //ROOT
		}
		List<TreeDataModel> treeNodes = new ArrayList<TreeDataModel>();
		List<Catalog> catalogs = catalogService.findCatalogByParentId(parentId);
		for (Catalog Catalog : catalogs) {
			TreeDataModel catalogTree = new TreeDataModel();
			catalogTree.setId(Catalog.getId());
			catalogTree.setText(Catalog.getCnName());
			List<TreeDataModel> children = queryCatalogListByParentId(Catalog.getId());
			
			if(children == null && Catalog.getIsleaf()){
				catalogTree.setChildren(null);
			}else{
				catalogTree.setChildren(children);
			}
			treeNodes.add(catalogTree);
		}
		return treeNodes;
	}
}
